package controller;

import bean.dto.Dto;
import bean.pojo.GdDoctor;
import bean.vo.DoctorVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.GdDoctorService;
import service.TokenService;
import utils.DtoUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/docList")
public class DoctorController {
    @Resource
    private GdDoctorService gdDoctorService;
    @Resource
    private TokenService tokenService;

    /**
     * 查询所有医生 + 排序(by 服务人数和评分)
     * @param orderBy 1.综合 2.服务人数 3. 评分
     * @param request
     * @return
     */
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public @ResponseBody Dto selectAllDoc(@Param("orderBy")int orderBy,
                                          HttpServletRequest request){
//        String token = request.getHeader("token");
        try {
//            if (tokenService.validate(request.getHeader("user-agent"),token)){
                GdDoctor gdDoctor = new GdDoctor();
                List<GdDoctor> doctorList = null;
                if(orderBy==1){
                    doctorList = gdDoctorService.select();
                }
                if(orderBy==2){
                    doctorList = gdDoctorService.selectWithOrder("servicenum");
                }
                if(orderBy==3){
                    doctorList = gdDoctorService.selectWithOrder("evaluationScore");
                }
                if (doctorList!=null){
                    return DtoUtil.returnSuccessWithData("查询全部医生",doctorList);
                }
                return DtoUtil.returnFail("error","查询失败");
//            }
//            return DtoUtil.returnFail("error","token已失效");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","查询失败");
        }
    }

    /**
     * 通过科室/名字查询医生
     * @return
     */
    @RequestMapping(value = "/byCondition",method = RequestMethod.POST)
    public @ResponseBody Dto selectByDepartment(@RequestBody DoctorVo doctorVo,
                                                HttpServletRequest request){
//        String token = request.getHeader("token");
        try {
//            if(tokenService.validate(request.getHeader("user-agent"),token)){
                List<GdDoctor> doctorList = null;
                GdDoctor gdDoctor = new GdDoctor();
                if (doctorVo.getDepartmentId()!=0&&doctorVo.getDepartmentId()!=null)
                    doctorList = gdDoctorService.selectByCondition("department_id",doctorVo.getDepartmentId());
                int length = doctorVo.getDoctorName().length();
                if (length>0&&doctorVo.getDoctorName()!=null)
                    doctorList = gdDoctorService.selectByCondition("name",doctorVo.getDoctorName());
                if (doctorList!=null)
                    return DtoUtil.returnSuccessWithData("查询成功",doctorList);
                return DtoUtil.returnFail("error","查询失败");
//            }
//            return DtoUtil.returnFail("error","token失效");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","查询失败");
        }
    }

}
